﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources; // EnumerableDataSource
using Microsoft.Research.DynamicDataDisplay.PointMarkers; // CirclePointMarker

namespace MapGraph
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      Loaded += new RoutedEventHandler(Window1_Loaded);
    }

    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
      List<MapInfo> mapInfoList = LoadMapInfo("..\\..\\usa13509.tsp");
      int[] ids = new int[mapInfoList.Count];
      double[] xs = new double[mapInfoList.Count];
      double[] ys = new double[mapInfoList.Count];

      for (int i = 0; i < mapInfoList.Count; ++i)
      {
        ids[i] = mapInfoList[i].id;
        xs[i] = mapInfoList[i].lon; // x-values are longitude
        ys[i] = mapInfoList[i].lat; // y-values are latitude
      }

      var xDataSource = new EnumerableDataSource<double>(xs);
      xDataSource.SetXMapping(x => Convert.ToDouble(x));

      var yDataSource = new EnumerableDataSource<double>(ys);
      yDataSource.SetYMapping(y => y);

      CompositeDataSource compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

      plotter.AddLineGraph(compositeDataSource,
                new Pen(Brushes.White, 0),
                new CirclePointMarker { Size = 2.0, Fill = Brushes.Red },
                new PenDescription("U.S. cities"));

      plotter.Viewport.FitToView();

    }

    private static List<MapInfo> LoadMapInfo(string fileName)
    {
      var result = new List<MapInfo>();
      FileStream fs = new FileStream(fileName, FileMode.Open);
      StreamReader sr = new StreamReader(fs);

      string line = "";
      while ((line = sr.ReadLine()) != null)
      {
        line = line.ToString().Trim();
        if (line.StartsWith("//"))
          continue;
        else
        {
          string[] pieces = line.Split(' ');
          int id = int.Parse(pieces[0]);
          double lat = double.Parse(pieces[1]); // 
          double lon = -1.0 * double.Parse(pieces[2]); // 
          MapInfo mi = new MapInfo(id, lat, lon);
          result.Add(mi);
        }
      }
      sr.Close();
      fs.Close();
      return result;
    }

  } // class Window1

  public class MapInfo
  {
    public int id;
    public double lat;
    public double lon;

    public MapInfo(int id, double lat, double lon)
    {
      this.id = id; this.lat = lat; this.lon = lon;
    }
  }
  
} // ns
